package zd.cornermemory.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by zhud on 2021/6/11.
 */

public class FourCube {
    static Random random = new Random();
    static List<Integer> cubeData = new ArrayList<>();
    static List<Integer> cubeNumbers = new ArrayList<>();
    static List<Integer> numbers = new ArrayList<>();


    static String getWingData(int position){
        String s = "";
        switch (position) {
            case 0:
                s = "E";
                break;
            case 1:
                s = "F";
                break;
            case 2:
                s = "D";
                break;
            case 3:
                s = "G";
                break;
            case 4:
                s = "C";
                break;
            case 5:
                s = "H";
                break;
            case 6:
                s = "B";
                break;
            case 7:
                s = "A";
                break;
            case 8:
                s = "Q";
                break;
            case 9:
                s = "Z";
                break;
            case 10:
                s = "R";
                break;
            case 11:
                s = "Y";
                break;
            case 12:
                s = "O";
                break;
            case 13:
                s = "P";
                break;
            case 14:
                s = "T";
                break;
            case 15:
                s = "S";
                break;
            case 16:
                s = "I";
                break;
            case 17:
                s = "J";
                break;
            case 18:
                s = "L";
                break;
            case 19:
                s = "K";
                break;
            case 20:
                s = "N";
                break;
            case 21:
                s = "M";
                break;
            case 22:
                s = "W";
                break;
            case 23:
                s = "X";
                break;
        }
        return s;
    }


    public static String getPhaseStr(boolean isXCenter){
        String str = "";
        cubeData.clear();
        cubeNumbers.clear();
        numbers.clear();

        for(int i = 0;i<24;i++) {
            cubeData.add(-1);
        }

        for(int i = 0;i<24;i++) {
            cubeNumbers.add(i);
        }

        for(int i = 0;i<24;i++) {
            numbers.add(i);
        }


        if(isXCenter){
            //先随机贴4个归位的中心，模拟四盲开盖即坐标，不然可能生成的中心未归位数过多
            int count = 4;
            while (count > 0) {
                //取出随机数
                int randomInt = random.nextInt(numbers.size());
                int offset = numbers.get(randomInt);
                delete(numbers, offset);

                cubeData.remove(offset);
                cubeData.add(offset,offset);
                delete(cubeNumbers, offset);
                count--;

            }
        }


        while (numbers.size() > 0) {
            //取出随机数
            int randomInt = random.nextInt(numbers.size());
            int offset = numbers.get(randomInt);
            delete(numbers, offset);


            int sourceInt = random.nextInt(cubeNumbers.size());
            int sourceOffset = cubeNumbers.get(sourceInt);
            delete(cubeNumbers, sourceOffset);

            cubeData.remove(sourceOffset);
            cubeData.add(sourceOffset,offset);
        }

        for (int a : cubeData) {
            if (a == -1) {
                System.out.print("error");
                break;
            }
        }

        if (isXCenter) {
            //打印归位个数
            int you = 0;
            for(int i=0;i<cubeData.size();i++) {
                String s = getStr(i);
                String s2 = getStr(cubeData.get(i));
                if (s.equals(s2)) {
                    you++;
                }
            }

//            System.out.println("角心归位个数 "+you);

            str += "UUUUU";
            str += getStr(cubeData.get(0));
            str += getStr(cubeData.get(1));
            str += "UU";
            str += getStr(cubeData.get(2));
            str += getStr(cubeData.get(3));
            str += "UUUUU";

            str += "RRRRR";
            str += getStr(cubeData.get(4));
            str += getStr(cubeData.get(5));
            str += "RR";
            str += getStr(cubeData.get(6));
            str += getStr(cubeData.get(7));
            str += "RRRRR";

            str += "FFFFF";
            str += getStr(cubeData.get(8));
            str += getStr(cubeData.get(9));
            str += "FF";
            str += getStr(cubeData.get(10));
            str += getStr(cubeData.get(11));
            str += "FFFFF";

            str += "DDDDD";
            str += getStr(cubeData.get(12));
            str += getStr(cubeData.get(13));
            str += "DD";
            str += getStr(cubeData.get(14));
            str += getStr(cubeData.get(15));
            str += "DDDDD";

            str += "LLLLL";
            str += getStr(cubeData.get(16));
            str += getStr(cubeData.get(17));
            str += "LL";
            str += getStr(cubeData.get(18));
            str += getStr(cubeData.get(19));
            str += "LLLLL";

            str += "BBBBB";
            str += getStr(cubeData.get(20));
            str += getStr(cubeData.get(21));
            str += "BB";
            str += getStr(cubeData.get(22));
            str += getStr(cubeData.get(23));
            str += "BBBBB";
        }else{
            //打印归位个数
            int you = 0;
            for(int i=0;i<cubeData.size();i++) {
                if(i==cubeData.get(i)){
                    you++;
                }
            }

//            System.out.println("翼棱归位个数 "+you);

            str += "U";
            str += getWingStr(getWingData(cubeData.get(0)),false);
            str += getWingStr(getWingData(cubeData.get(1)),true);
            str += "U";
            
            str += getWingStr(getWingData(cubeData.get(2)),true);
            str += "UU";
            str += getWingStr(getWingData(cubeData.get(3)),false);
            
            str += getWingStr(getWingData(cubeData.get(4)),false);
            str += "UU";
            str += getWingStr(getWingData(cubeData.get(5)),true);

            str += "U";
            str += getWingStr(getWingData(cubeData.get(6)),true);
            str += getWingStr(getWingData(cubeData.get(7)),false);
            str += "U";



            str += "R";
            str += getWingStr(getWingData(cubeData.get(5)),false);
            str += getWingStr(getWingData(cubeData.get(3)),true);
            str += "R";

            str += getWingStr(getWingData(cubeData.get(8)),true);
            str += "RR";
            str += getWingStr(getWingData(cubeData.get(9)),false);

            str += getWingStr(getWingData(cubeData.get(10)),false);
            str += "RR";
            str += getWingStr(getWingData(cubeData.get(11)),true);

            str += "R";
            str += getWingStr(getWingData(cubeData.get(12)),true);
            str += getWingStr(getWingData(cubeData.get(13)),false);
            str += "R";


            str += "F";
            str += getWingStr(getWingData(cubeData.get(6)),false);
            str += getWingStr(getWingData(cubeData.get(7)),true);
            str += "F";

            str += getWingStr(getWingData(cubeData.get(14)),true);
            str += "FF";
            str += getWingStr(getWingData(cubeData.get(8)),false);

            str += getWingStr(getWingData(cubeData.get(15)),false);
            str += "FF";
            str += getWingStr(getWingData(cubeData.get(10)),true);

            str += "F";
            str += getWingStr(getWingData(cubeData.get(16)),true);
            str += getWingStr(getWingData(cubeData.get(17)),false);
            str += "F";


            str += "D";
            str += getWingStr(getWingData(cubeData.get(16)),false);
            str += getWingStr(getWingData(cubeData.get(17)),true);
            str += "D";

            str += getWingStr(getWingData(cubeData.get(18)),true);
            str += "DD";
            str += getWingStr(getWingData(cubeData.get(12)),false);

            str += getWingStr(getWingData(cubeData.get(19)),false);
            str += "DD";
            str += getWingStr(getWingData(cubeData.get(13)),true);

            str += "D";
            str += getWingStr(getWingData(cubeData.get(20)),true);
            str += getWingStr(getWingData(cubeData.get(21)),false);
            str += "D";


            str += "L";
            str += getWingStr(getWingData(cubeData.get(2)),false);
            str += getWingStr(getWingData(cubeData.get(4)),true);
            str += "L";

            str += getWingStr(getWingData(cubeData.get(22)),true);
            str += "LL";
            str += getWingStr(getWingData(cubeData.get(14)),false);

            str += getWingStr(getWingData(cubeData.get(23)),false);
            str += "LL";
            str += getWingStr(getWingData(cubeData.get(15)),true);

            str += "L";
            str += getWingStr(getWingData(cubeData.get(19)),true);
            str += getWingStr(getWingData(cubeData.get(18)),false);
            str += "L";


            str += "B";
            str += getWingStr(getWingData(cubeData.get(1)),false);
            str += getWingStr(getWingData(cubeData.get(0)),true);
            str += "B";

            str += getWingStr(getWingData(cubeData.get(9)),true);
            str += "BB";
            str += getWingStr(getWingData(cubeData.get(22)),false);

            str += getWingStr(getWingData(cubeData.get(11)),false);
            str += "BB";
            str += getWingStr(getWingData(cubeData.get(23)),true);

            str += "B";
            str += getWingStr(getWingData(cubeData.get(21)),true);
            str += getWingStr(getWingData(cubeData.get(20)),false);
            str += "B";
            
        }

        return str;
    }

    private static boolean delete(List<Integer> numbers, int offset) {
        int index = -1;
        for(int i = 0;i<numbers.size();i++) {
            int a = numbers.get(i);
            if (a == offset) {
                index = i;
                break;
            }
        }

        if (index != -1) {
            numbers.remove(index);
            return true;
        }else{
            return false;
        }
    }

    private static String getStr(int number){
        String s = "";
        if(number==0||number==1||number==2||number==3){
            s = "U";
        }
        if(number==4||number==5||number==6||number==7){
            s = "R";
        }
        if(number==8||number==9||number==10||number==11){
            s = "F";
        }
        if(number==12||number==13||number==14||number==15){
            s = "D";
        }
        if(number==16||number==17||number==18||number==19){
            s = "L";
        }
        if(number==20||number==21||number==22||number==23){
            s = "B";
        }

        return s;
    }

    private static String getWingStr(String number,boolean isFan){
        String s = "";


        if (isFan) {
            if(number.equals("E")||number.equals("Z")||number.equals("M")||number.equals("X")){
                s = "B";
            }

            if(number.equals("B")||number.equals("D")||number.equals("F")||number.equals("H")){
                s = "U";
            }

            if(number.equals("G")||number.equals("Q")||number.equals("Y")||number.equals("O")){
                s = "R";
            }

            if(number.equals("C")||number.equals("S")||number.equals("K")||number.equals("W")){
                s = "L";
            }

            if(number.equals("J")||number.equals("L")||number.equals("N")||number.equals("P")){
                s = "D";
            }

            if(number.equals("A")||number.equals("T")||number.equals("R")||number.equals("I")){
                s = "F";
            }

        }else{
            if(number.equals("E")||number.equals("C")||number.equals("G")||number.equals("A")){
                s = "U";
            }
            if(number.equals("H")||number.equals("Z")||number.equals("R")||number.equals("P")){
                s = "R";
            }
            if(number.equals("B")||number.equals("Q")||number.equals("S")||number.equals("J")){
                s = "F";
            }
            if(number.equals("I")||number.equals("O")||number.equals("K")||number.equals("M")){
                s = "D";
            }
            if(number.equals("D")||number.equals("T")||number.equals("X")||number.equals("L")){
                s = "L";
            }
            if(number.equals("F")||number.equals("W")||number.equals("Y")||number.equals("N")){
                s = "B";
            }
        }

        return s;
    }

}
